package cn.itsource.pethome.quartz.job;

import cn.itsource.pethome.order.domain.ProductOrder;
import cn.itsource.pethome.order.mapper.ProductOrderMapper;
import cn.itsource.pethome.pay.domain.PayBill;
import cn.itsource.pethome.pay.mapper.PayBillMapper;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * 业务处理类
 */
public class QuartzJob implements Job {
    //订单
    @Autowired
    private ProductOrderMapper productOrderMapper;
    //支付
    @Autowired
    private PayBillMapper payBillMapper;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Object params = jobExecutionContext.getMergedJobDataMap().get("params");
        //将获取到的params转换为QuartzJobInfo对象
        QuartzJobInfo info = (QuartzJobInfo) params;
        //获取订单编号
        Map<String, Object> map = info.getParams();
        //获取订单编号
        String orderSn = (String) map.get("orderSn");
        //根据订单号查询订单对象
        ProductOrder productOrder = productOrderMapper.loadByOrderSn(orderSn);
        //设置为已取消
        productOrder.setState(-1);
        //更新
        productOrderMapper.update(productOrder);

        //根据订单号查询支付订单
        PayBill payBill = payBillMapper.loadByOrderSn(orderSn);
        //设置已取消
        payBill.setState(-1);
        //设置更新时间
        payBill.setUpdateTime(new Date());
        //更新
        payBillMapper.update(payBill);
    }
}
